<?php

namespace App\Console\Commands;

use App\Models\Pinpai;
use App\Models\Industry;
use App\Models\Brand;
use Illuminate\Console\Command;

class ReorganizeIndustry extends Command {
    
    protected $signature = 'ri';
    protected $description = 'ReorganizeIndustry';
    
    public function __construct() {
        parent::__construct ();
    }
    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function handle() {
        $this->error('开始整理行业数据');
        $industries = Pinpai::select('hangye')->groupBy('hangye')->get();
        $this->saveIndustry($industries);
    }
    
    public function saveIndustry($industries) {
        $i = 1;
        $count = count($industries);
        foreach ($industries as $industry) {
            $c = Industry::where('name', $industry->hangye)->first();
            $num = $i.'/'.$count;
            if(is_null($c)) {
                $c = new Industry;
                $c->name = $industry->hangye;
                $c->save();
                $this->info('行业：'.$num.'  '.$c->name);
            }
            $i++;
            unset($c);
        }
    }
}
